package cn.yiyuan;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MusicSearch {

    public static void main(String[] args) {
        String apiUrl = "https://v2.alapi.cn/api/music/search";
        String token = "LwExDtUWhF3rH5ib"; // 替换为实际的 token
        String keyword = "爱你一万年"; // 替换为实际的歌曲名

        try {
            // 构建请求URL
            String requestUrl = apiUrl + "?keyword=" + keyword + "&token=" + token;

            // 创建 URL 对象
            URL url = new URL(requestUrl);

            // 创建 HttpURLConnection 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // 获取响应代码
            int responseCode = connection.getResponseCode();

            // 如果响应代码是 200 表示请求成功
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取响应数据
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }

                // 关闭资源
                reader.close();

                // 处理 JSON 响应
                parseJsonResponse(response.toString());
            } else {
                System.out.println("HTTP request failed with response code: " + responseCode);
            }

            // 关闭连接
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void parseJsonResponse(String jsonResponse) {
        // 在这里解析 JSON 响应并提取所需的信息
        // 这里只是一个简单的示例，请根据实际情况进行适当的解析
        System.out.println("JSON Response: " + jsonResponse);
    }
}
